<template>
  <div class="page-login">
    <div class="center-form">
      <h2>登录</h2>
      <!-- 表单 -->
      <el-form :model="ruleForm"
               ref="loginForm"
               :rules="rules"
               label-width="80px">
        <!-- TODO:账号 -->
        <el-form-item label="账号 :"
                      prop="username">
          <el-input type="text"
                    v-model="ruleForm.username"
                    placeholder="输入账号"></el-input>
        </el-form-item>
        <!-- TODO:密码 -->
        <el-form-item label="密码 :"
                      prop="password">
          <el-input type="password"
                    v-model="ruleForm.password"
                    placeholder="输入密码"
                    show-password></el-input>
        </el-form-item>
        <!-- TODO:验证码 -->
        <el-form-item label="验证码 :"
                      class="capImg">
          <el-input v-model="ruleForm.code"
                    placeholder="验证码"
                    >
          </el-input>
          <el-image style="width: 80px; height: 40px"
                    :src="captchaImg"
                    @click="getCaptcha"></el-image>
        </el-form-item>
        <!-- 跳转登录 -->
        <div class="go-login">
          <a @click.prevent="goRegisterPage">没有账号？注册</a>
        </div>
        <!-- 注册按钮 -->
        <div class="submit">
          <el-button @click="submitForm('loginForm')">确认登录</el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import { setToken } from '@/request/token'
import { captcha, login } from '@/api/user' // 请求方法
import qs from 'qs'
export default {
  data() {
    return {
      //表单数据
      ruleForm: {
        username: '',
        password: '',
        code: '',
        token: ''
      },
      captchaImg: '',
      // 校验规则
      rules: {
        username: [{ required: true, message: '账号不能为空', trigger: 'blur' }],
        password: [{ required: true, message: '密码不能为空', trigger: 'blur' }]
      }
    }
  },
  methods: {
    // 提交表单处理函数
    submitForm(ruleForm) {
      this.$refs[ruleForm].validate(valid => {
        // 校验通过
        if (valid) {
          login(qs.stringify(this.ruleForm)).then(res => {
            setToken(res.headers['authorization'])
            this.$router.push('/')
            this.$message('success','登录成功')
          })
        } else {
          return false
        }
      })
    },
    // 前往登录页面
    goRegisterPage() {
      this.$router.push('/register')
    },
    //获取验证码
    async getCaptcha() {
      let { data: res } = await captcha()
      if (res.code == 200) {
        this.ruleForm.token = res.data.token
        this.captchaImg = res.data.captchaImg
        this.ruleForm.code = ''
      }
    }
  },
  created() {
    this.getCaptcha()
  }
}
</script>

<style lang="less" scoped>
.page-login {
  position: relative;
  /* 页面背景图 */
  width: 100%;
  height: 100vh;
  background: url('@/assets/images/login-bgi.jpg') no-repeat;
  background-size: 100% 100%;
  .center-form {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    padding-bottom: 30px;
    width: 350px;
    box-sizing: border-box;
    border-radius: 20px;
    background-color: #fff;
    h2 {
      text-align: center;
      margin: 20px 0 25px 0;
      letter-spacing: 20px;
      font-size: 29px;
      text-indent: 20px;
      color: #34c5c5;
    }
    :deep(.capImg) {
      .el-form-item__content {
        display: flex;
        .el-input {
          width: 50%;
          .el-input__inner {
            width: 100%;
          }
        }
        .el-image {
          margin-left: 10px;
          cursor: pointer;
        }
      }
    }
    /* 表单 */
    :deep(.el-form-item) {
      margin-bottom: 20px;
      /* label样式修改 */
      .el-form-item__label {
        color: #000;
        position: relative;
        /* *号修改 */
        &::before {
          position: absolute;
          right: 47px;
          top: 1px;
          color: #ff0000;
        }
      }
    }
    /* 表单输入框 */
    :deep(.el-input) {
      width: 85%;
      .el-input__inner {
        width: 230px;
        &:focus {
          border: 1px solid #34c5c5;
        }
      }
    }

    /* 已有账号，跳转到登录页面 */
    .go-login {
      padding-right: 38px;
      text-align: right;
      font-size: 12px;
      a {
        color: #34c5c5;
        cursor: pointer;
      }
    }
    /* 登录 */
    :deep(.submit) {
      margin-top: 25px;
      display: flex;
      justify-content: center;
      .el-button {
        width: 40%;
        padding: 10px 0;
        letter-spacing: 3px;
        font-size: 16px;
        color: #fff;
        cursor: pointer;
        border: none;
        border-radius: 4px;
        background-color: #34c5c5;
        span {
          color: #fff;
        }
      }
    }
  }
}
</style>